package com.tamo.ppresta.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import java.util.Map;

/**
 * @author yeyang
 * @name ppresta
 * @class name：com.tamo.ppresta.util
 * @class describe
 * @time 2022/12/22 20:46
 * @change
 * @chang time
 * @class describe
 */
public class SPUtil {
    /**
     * 保存数据
     *
     * @param context
     * @param name
     * @param key
     * @param value
     */
    public static void saveObject(Context context, String name, String key, Object value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        // 以不同的类型来判断保存
        if (value instanceof String) {
            edit.putString(key, (String) value);
        } else if (value instanceof Long) {
            edit.putLong(key, (Long) value);
        } else if (value instanceof Integer) {
            edit.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            edit.putBoolean(key, (Boolean) value);
        }
        edit.commit();
    }

    /**
     * 提取String类型数据
     *
     * @param context
     * @param name
     * @param key
     * @return
     */
    public static String getString(Context context, String name, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, context.MODE_PRIVATE);
        String str = sharedPreferences.getString(key, "");
        return str;
    }

    /**
     * 提取int类型数据
     *
     * @param context
     * @param name
     * @param key
     * @return
     */
    public static int getInt(Context context, String name, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, context.MODE_PRIVATE);
        int str = sharedPreferences.getInt(key, 0);
        return str;
    }

    /**
     * 提取long类型数据
     *
     * @param context
     * @param name
     * @param key
     * @return
     */
    public static long getLong(Context context, String name, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, context.MODE_PRIVATE);
        long str = sharedPreferences.getLong(key, 0L);
        return str;
    }

    /**
     * 提取boolean类型数据
     *
     * @param context
     * @param name
     * @param key
     * @return
     */
    public static boolean getBoolean(Context context, String name, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, context.MODE_PRIVATE);
        boolean str = sharedPreferences.getBoolean(key, false);
        return str;
    }

    /**
     * 保存bean对象
     *
     * @param context
     * @param name
     * @param key
     * @param obj
     */
    public static void saveBeanByFastJson(Context context, String name, String key, Object obj) {
        SharedPreferences.Editor editor = (SharedPreferences.Editor) context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
        String toJson = new Gson().toJson(obj);
        editor.putString(key, toJson).commit();
    }

    /**
     * 提取bean对象
     *
     * @param context
     * @param name
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBeanByFastJson(Context context, String name, String key, Class<T> clazz) {
        String string = context.getSharedPreferences(name, Context.MODE_PRIVATE).getString(key, "");
        return new Gson().fromJson(string, clazz);
    }

    /**
     * 查询某个key是否已经存在
     *
     * @param sp  SharedPreferences实例
     * @param key
     * @return
     */
    public static boolean contains(SharedPreferences sp, String key) {
        return sp.contains(key);
    }

    /**
     * 返回所有的键值对
     *
     * @param sp SharedPreferences实例
     * @return
     */
    public static Map<String, ?> getAll(SharedPreferences sp) {
        try {
            return sp.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 去除某一键值对
     *
     * @param context
     * @param name
     * @param key
     * @return
     */
    public static boolean remove(Context context, String name, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, context.MODE_PRIVATE);
        return sharedPreferences.edit().remove(key).commit();
    }

    /**
     * 清空销毁
     *
     * @param context
     */
    public static boolean clear(Context context, String name) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(name, context.MODE_PRIVATE);
        return sharedPreferences.edit().clear().commit();
    }
} 